<?php
namespace app\service;
use alipay\AopClient;
use alipay\AlipayOpenAuthTokenAppRequest;    //查询物业token
use alipay\AlipaySystemOauthTokenRequest;//换取授权访问令牌
use alipay\AlipayTradeCreateRequest;//生成订单
use alipay\AlipayTradeCloseRequest;
use alipay\AlipayOpenAppMiniTemplatemessageSendRequest;//小程序模板信息
use alipay\AlipayOpenAppQrcodeCreateRequest;//创建二维码
use alipay\ZolozIdentificationCustomerCertifyzhubQueryRequest;
use alipay\ZolozIdentificationUserWebQueryRequest;
class apixcxService{
    const PRIKEYPATH = 'C:\phpStudy\WWW\zaozao\prikey.txt';//私钥路径
    const PUBKEYPATH = 'C:\phpStudy\WWW\zaozao\pubkey.txt';//支付宝公钥路径

    const XCXPRIKEYPATH = 'C:\phpStudy\WWW\zaozao\xcxprikey.txt';//私钥路径
    const XCXPUBKEYPATH = 'C:\phpStudy\WWW\zaozao\xcxpubkey.txt';//支付宝公钥路径

    const WUYEPRIKEYPATH = 'C:\phpStudy\WWW\zaozao\prikeywuye.txt';//私钥路径
    const WUYEPUBKEYPATH = 'C:\phpStudy\WWW\zaozao\pubkeywuye.txt';//支付宝公钥路径

    private $app_id;//支付宝分配给开发者的应用
    private $method;//接口名称
    private $format;//仅支持JSON  no
    private $charset = 'UTF-8';//请求使用的编码格式，如utf-8,gbk,gb2312等
    private $sign_type = 'RSA2';//商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2
    private $sign;//商户请求参数的签名串  no
    private $timestamp;//发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"
    private $version = '1.0';//调用的接口版本，固定为：1.0
    private $app_auth_token;//详见应用授权概述 no
    private $biz_content;//

    public function getPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018031602386173';
        $aop->rsaPrivateKey = file_get_contents(self::PRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::PUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }
     //XIOA
    public function xcxgetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018110562021261';
        $aop->rsaPrivateKey = file_get_contents(self::XCXPRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::XCXPUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }

    //小程序换取授权访问令牌
    public function xcxoauthToken($code){
        $aop = $this->xcxgetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ($request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";   
        }
        return $result->$responseNode;
    }

    public function lifegetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2019070965840029';
        $aop->rsaPrivateKey = file_get_contents(self::WUYEPRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::WUYEPUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }

    //换取授权访问令牌
    public function oauthToken($code){
        $aop = $this->lifegetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ( $request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        }
        return $result->$responseNode;
    }

    //生成订单
    public  function create($data){
        $data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayTradeCreateRequest ();
        $request->setBizContent("{" .
        "\"out_trade_no\":\"$data->out_trade_no\"," .
        "\"total_amount\":$data->total_amount," .
        "\"subject\":\"$data->subject\"," .
        "\"buyer_id\":\"$data->buyer_id\"," .
        "\"extend_params\":{".
            "\"sys_service_provider_id\":\"2088031574378734\"".
            "},".
        "\"timeout_express\":\"100m\"" .
        "  }");
        $request->setNotifyUrl($data->notifyUrl);
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
	public function close($data){
        $data = json_decode($data);
        $aop = $this->getPublicContent();
        $request = new AlipayTradeCloseRequest ();
        $request->setBizContent("{" .
            "\"trade_no\":\"$data->trade_no\"" .
            "  }");
        
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    //
    public function appletSendMsg($data){
        $request = new AlipayOpenAppMiniTemplatemessageSendRequest ();
        $request->setBizContent("{" .
        "\"to_user_id\":\"$data->user_id\"," .
        "\"form_id\":\"$data->form_id\"," .
        "\"user_template_id\":\"$data->template_id\"," .
        "\"page\":\"$data->page\"," .
        "\"data\":\"$data->data\"" .
        "}");
        $result = $aop->execute ( $request); 
         
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
    public function  online(){

        $aop = $this->getPublicContent();
        $request = new AlipayOpenMiniVersionOnlineRequest ();
        $request->setBizContent("{" .
        "\"app_version\":\"0.0.3\"" .
        "  }");
        $result = $aop->execute ( $request); 

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    public function qrcodeCreate($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new AlipayOpenAppQrcodeCreateRequest ();
        $request->setBizContent("{" .
        "\"url_param\":\"$data->url_param\"," .
        "\"query_param\":\"$data->query_param\"," .
        "\"describe\":\"$data->describe\"" .
        "  }");
        $result = $aop->execute ( $request); 
         
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    public function faceimgquery($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new ZolozIdentificationUserWebQueryRequest ();
        $request->setBizContent("{" .
        "\"biz_id\":\"$data->biz_id\"," .
        "\"zim_id\":\"$data->zim_id\"," .
        "\"extern_param\":\"{}\"" .
        "  }");
        $result = $aop->execute ( $request); 
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
    public function customerquery($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new ZolozIdentificationCustomerCertifyzhubQueryRequest ();
        $request->setBizContent("{" .
        "\"biz_id\":\"$data->biz_id\"," .
        "\"zim_id\":\"$data->zim_id\"," .
        "\"face_type\":0," .
        "\"need_img\":true" .
        "  }");
        $result = $aop->execute ( $request); 
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
}